package com.ciaojian.client.service;


import com.ciaojian.core.enums.business.MemberOrderTypeEnum;
import com.ciaojian.core.mapper.MemberOrderMapper;
import com.ciaojian.core.mapper.MyCouponMapper;
import com.ciaojian.core.mapper.UserMemberMapper;
import com.ciaojian.core.mapper.WorkerMapper;
import com.ciaojian.core.model.Member;
import com.ciaojian.core.model.MemberOrder;
import com.ciaojian.core.model.UserMember;
import com.ciaojian.core.service.MemberOrderService;
import com.ciaojian.core.util.LogUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Atlakyn
 */
@Service
public class MemberOrderServiceImpl extends MemberOrderService<MemberOrderMapper, MemberOrder> {
    @Resource
    private MyCouponMapper myCouponMapper;
    @Resource
    private WorkerMapper workerMapper;
    @Resource
    private MemberOrderMapper memberOrderMapper;
    @Resource
    private UserMemberServiceImpl<UserMemberMapper, UserMember> userMemberService;
    @Resource
    private MemberServiceImpl memberService;

    /**
     * 会员卡订单支付回调
     *
     * @param memberOrder 会员订单
     */
    public void wxNotifyOfMemberCard(MemberOrder memberOrder) {
        LogUtil.writePayLog(">>>wxNotifyOfMemberCard>>>orderNo={}", memberOrder);

        // 支付成功 修改订单为已支付状态
        memberOrderMapper.updateMemberOrderStatus(memberOrder.getOrderNo());
        // 如果用户之前购买过续费逻辑如下
        Integer memBerValid = memberService.getBaseMapper().getMemBerValid(memberOrder.getMemberId());
        // 查询用户是否购买过会员卡
        UserMember userMember = userMemberService.getBaseMapper().getUserMember(memberOrder.getUserId(), memberOrder.getMemberId());
        // 没有购买过
        if (userMember == null) {
            // 新增会员和用户关联信息
            userMember = new UserMember();
            userMember.setUserId(memberOrder.getUserId());
            userMember.setDueDate(new Date());
            userMember.setMemberId(memberOrder.getMemberId());
            userMember.setOrderNo(memberOrder.getOrderNo());
            memberService.getBaseMapper().saveUserMember(userMember);
        }
        // 已购买
        else {
            // 对比两种会员卡的折扣谁低
            if (!memberOrder.getMemberId().equals(userMember.getMemberId())) {
                // 购买的会员卡
                Member memberBuy = new Member().selectById(memberOrder.getMemberId());
                // 拥有的会员卡
                Member memberOwned = new Member().selectById(userMember.getMemberId());

                try {
                    Member member = memberService.compareMember(memberBuy, memberOwned);
                    userMemberService.getBaseMapper().updateMemberIdByUserId(memberOrder.getUserId(), member.getId());
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }

            Integer expiredCount = memberService.getBaseMapper().memberExpiredGreater(memberOrder.getUserId(), memberOrder.getMemberId());
            // 会员过期再次购买
            if (expiredCount < 1) {
                LogUtil.writePayLog(">>>wxNotifyOfMemberCard>>>会员过期");
                memberService.getBaseMapper().memberExpired(memberOrder.getMemberId(), memberOrder.getUserId());
            }


        }
        // 修改会员卡日期-续费1年
        memberService.getBaseMapper().addMemBerMonth(memberOrder.getUserId(), memberOrder.getMemberId(), memBerValid);
    }

    public MemberOrder insert(Integer userId, Integer memberId, Integer inviteId, String orderNo) {
        // 新增会员卡订单
        MemberOrder memberOrder = new MemberOrder();
        memberOrder.setUserId(userId);
        memberOrder.setMemberId(memberId);
        memberOrder.setInviteId(inviteId);
        memberOrder.setStoreId(workerMapper.getStoreIdById(inviteId));
        memberOrder.setOrderNo(orderNo);
        Integer memBerCount = myCouponMapper.getMemBerCount(userId.toString(), memberId);
        if (memBerCount > 0) {
            memberOrder.setType(MemberOrderTypeEnum.renew);
        } else {
            memberOrder.setType(MemberOrderTypeEnum.non_renew);
        }

        Member dMember = memberService.getBaseMapper().selectById(memberId);
        memberOrder.setMoney(dMember.getPrice().longValue());
        memberService.getBaseMapper().saveByMemberOrder(memberOrder);
        return memberOrder;
    }

}